#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "../../mem/shm_mem.h"
#include "../../mem/mem.h"
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../parser/parse_uri.h"

#include "imc.h"
#include "imc_mng.h"
#include "imc_cmd.h"
#include "imc_http.h"


static str imc_msg_type = { "MESSAGE", 7 };

int imc_room_broadcast(imc_room_p room, str *ctype, str *body)
{/*{{{*/
    imc_member_p imp = NULL;

    if(NULL == room || NULL == body)
    {
        return -1;
    }

    imp = room->members;

    LM_DBG("nr = %d\n", room->nr_of_members);

    while(imp)
    {
        LM_DBG("broadcast to uri = [%.*s] and body = [%.*s]\n", imp->uri.len, imp->uri.s, body->len, body->s);
        if(IMC_MEMBER_SKIP & imp->flags)
        {
            imp = imp->next;
            continue;
        }

        imc_send_message(&room->uri, &imp->uri, ctype, body);
        imp = imp->next;
    }

    return 0;
}/*}}}*/

int imc_send_message(str *src, str *dst, str *headers, str *body)
{/*{{{*/
    uac_req_t uac_r;

    if(NULL == src || NULL == dst || NULL == body)
    {
        return -1;
    }

    LM_DBG("sent messages to [%.*s] from [%.*s] and body [%.*s]",dst->len, dst->s, src->len, src->s, body->len, body->s);
    set_uac_req(&uac_r, &imc_msg_type, headers, body, 0, 0, 0, 0); 
    tmb.t_request(&uac_r,
        NULL,
        dst,
        src,
        (outbound_proxy.s)?&outbound_proxy:NULL
        );

    return 0;
}/*}}}*/


int imc_handle_message(struct sip_msg* msg, str *msybody,
    struct sip_uri * src, struct sip_uri *dst)
{/*{{{*/
    imc_room_p   room   = NULL;
    imc_member_p member = NULL;

    room = imc_get_room(&dst->user, &dst->host);
    if(NULL == room)
    {
        LM_ERR("room [%.*s] does not exist!\n", dst->user.len, dst->user.s);
        //TODO:get member by http
        room = imc_http_inquire_room(&dst->user, &dst->host);
        if(room == NULL)
            goto error;
    }

    member = imc_get_member(room, &src->user, &src->host);
    if(NULL == member)
    {
        LM_ERR("user [%.*s] has no rightes to send messages in room [%.*s]!\n",
            src->user.len, src->user.s, dst->user.len, dst->user.s);
        //TODO:the sender is not in the room
        str dst_uri = {NULL, 0};

        dst_uri.len = 5 /* "sip:" + '@' */ + src->user.len + src->host.len;

        dst_uri.s = (char*)shm_malloc(dst_uri.len + 1);

        if(NULL == dst_uri.s)
        {
            LM_ERR("malloc the length[%d] of dst_uri out of shm memory in the imc_handle_message\n", dst_uri.len+1);
            goto error;
        }
        memcpy(dst_uri.s, "sip:", 4);
        memcpy(dst_uri.s+4, src->user.s, src->user.len);
        dst_uri.s[4+src->user.len] = '@';
        memcpy(dst_uri.s + 5 + src->user.len, src->host.s, src->host.len);
        dst_uri.s[dst_uri.len] = '\0';
        
        //info client don't to send messages
        str info = {"message send faild", 18};
        imc_send_message(&room->uri, &dst_uri, &all_hdrs, &info);
        shm_free(dst_uri.s);
        goto error;
    }


	member->flags |= IMC_MEMBER_SKIP;
	imc_room_broadcast(room, &all_hdrs, msybody);
	member->flags &= ~IMC_MEMBER_SKIP;

    if(NULL != room)
        imc_release_room(room);

    return 0;

error:
    if(NULL != room)
        imc_release_room(room);

    return -1;
}/*}}}*/

